package com.sansecy.android.simplevideo;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.SurfaceView;

public class VideoSurfaceView extends SurfaceView implements VideoViewInterface{

    public VideoSurfaceView(Context context) {
        super(context);
    }

    public VideoSurfaceView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    private int mVideoWidth = 0;
    private int mVideoHeight = 0;
    private int mVideoSarNum = 1;
    private int mVideoSarDen = 1;

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        //Log.i("@@@@", "onMeasure(" + MeasureSpec.toString(widthMeasureSpec) + ", "
        //        + MeasureSpec.toString(heightMeasureSpec) + ")");

        int width = getDefaultSize(mVideoWidth, widthMeasureSpec);
        int height = getDefaultSize(mVideoHeight, heightMeasureSpec);

        if (mVideoWidth > 0 && mVideoHeight > 0) {

            int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec);
            int widthSpecSize = MeasureSpec.getSize(widthMeasureSpec);
            int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec);
            int heightSpecSize = MeasureSpec.getSize(heightMeasureSpec);

            // Calculate display aspect ratio taking into account sample aspect ratio
            int displayWidth = mVideoWidth * mVideoSarNum;
            int displayHeight = mVideoHeight * mVideoSarDen;

            if (widthSpecMode == MeasureSpec.EXACTLY && heightSpecMode == MeasureSpec.EXACTLY) {
                // 大小是固定的
                width = widthSpecSize;
                height = heightSpecSize;
                Log.d(this.getClass().getName(), "onMeasure: 宽度大小 = " + widthSpecSize + " 宽度测量模式 EXACTLY" );
                Log.d(this.getClass().getName(), "onMeasure: 高度大小 = " + heightSpecSize + " 高度测量模式 EXACTLY" );

                // 为了兼容性，我们根据宽高比调整大小
                if (displayWidth * height < width * displayHeight) {
                    Log.i("@@@", "图像太宽，正在校正");
                    width = height * displayWidth / displayHeight;
                } else if (displayWidth * height > width * displayHeight) {
                    Log.i("@@@", "图像太高，正在校正");
                    height = width * displayHeight / displayWidth;
                }
            } else if (widthSpecMode == MeasureSpec.EXACTLY) {
                Log.d(this.getClass().getName(), "onMeasure: 宽度大小 = " + widthSpecSize + " 宽度测量模式 EXACTLY" );
                // 仅固定宽度，如果可能，调整高度以匹配宽高比
                width = widthSpecSize;
                height = width * displayHeight / displayWidth;
                if (heightSpecMode == MeasureSpec.AT_MOST && height > heightSpecSize) {
                    // 在约束内无法匹配宽高比
                    height = heightSpecSize;
                }
            } else if (heightSpecMode == MeasureSpec.EXACTLY) {
                Log.d(this.getClass().getName(), "onMeasure: 高度大小 = " + heightSpecSize + " 高度测量模式 EXACTLY" );
                // 仅固定高度​​，如果可能，调整宽度以匹配宽高比
                height = heightSpecSize;
                width = height * displayWidth / displayHeight;
                if (widthSpecMode == MeasureSpec.AT_MOST && width > widthSpecSize) {
                    // 在约束内无法匹配宽高比
                    width = widthSpecSize;
                }
            } else {
                Log.d(this.getClass().getName(), "onMeasure: 宽度大小 = " + widthSpecSize + " 宽度测量模式 未指定" );
                Log.d(this.getClass().getName(), "onMeasure: 高度大小 = " + heightSpecSize + " 高度测量模式 未指定" );
                // 宽度和高度均未固定，请尝试使用实际的视频尺寸
                width = mVideoWidth;
                height = mVideoHeight;
                if (heightSpecMode == MeasureSpec.AT_MOST && height > heightSpecSize) {
                    // 太高，减小宽度和高度
                    height = heightSpecSize;
                    width = height * displayWidth / displayHeight;
                }
                if (widthSpecMode == MeasureSpec.AT_MOST && width > widthSpecSize) {
                    // 太宽，减小宽度和高度
                    width = widthSpecSize;
                    height = width * displayHeight / displayWidth;
                }
            }
        } else {
            // 尚无尺寸，仅采用指定的规格尺寸
        }
        setMeasuredDimension(width, height);
    }

    @Override
    public void setVideoWidthHeight(int videoWidth, int videoHeight) {
        mVideoWidth = videoWidth;
        mVideoHeight = videoHeight;
        requestLayout();
    }

    @Override
    public void setVideoSampleAspectRatio(int sarNum, int sarDen) {
        mVideoSarNum = sarNum;
        mVideoSarDen = sarDen;
        requestLayout();
    }
}
